# Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
# easyAda is licensed under Mulan PubL v2.
# You can use this software according to the terms and conditions of the Mulan PubL v2.
# You may obtain a copy of Mulan PubL v2 at:
#          http://license.coscl.org.cn/MulanPubL-2.0
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PubL v2 for more details.

define check_empty
    ifeq ($$($(1)),)
        $$(error "$(1): empty")
    endif
endef

var_list = SOURCE_DIR
$(foreach var_name,$(var_list),$(eval $(call check_empty,$(var_name))))

ifeq ($(CONFIG_DIR),)
    CONFIG_DIR := $(SOURCE_DIR)
endif

# by platform
platforms := $(shell $(SOURCE_DIR)/scripts/supported-platforms.sh $(SOURCE_DIR))
platform_defconfigs := $(foreach var,$(platforms),plat_$(var)_defconfig)
.PHONY: $(platform_defconfigs)
$(platform_defconfigs) :
	${Q}cp -v $(patsubst plat_%_defconfig,$(SOURCE_DIR)/kernel/plat/%/defconfig,$@) $(CONFIG_DIR)/.config
	${Q}mkdir -p $(CONFIG_DIR)/include/generated
	${Q}cp -v $(patsubst plat_%_defconfig,$(SOURCE_DIR)/include/plat/%/plat/autoconf.h,$@) $(CONFIG_DIR)/include/generated/autoconf.h

.PHONY: defconfig
defconfig:
	$(Q)echo "support platforms:"
	$(Q)for plat in $(platforms); 	\
	do 					\
		echo " make plat_$${plat}_defconfig"; 	\
	done;

.PHONY : distclean
distclean:
	$(Q)rm -rf $(CONFIG_DIR)/include/generated
	$(Q)rm -rf $(CONFIG_DIR)/.config
	$(Q)echo " DIST CLEAN  kernel "
